/**
 * 
 */
package kr.pe.johnkim.dayve.rest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import kr.pe.johnkim.dayve.provider.DayveCategory;
import kr.pe.johnkim.dayve.provider.DayveItem;
import kr.pe.johnkim.dayve.util.ExternalJoinConstant;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

/**
 * 카테고리 RESTful 관련 기능들을 구현하는 클래스이다.
 * 
 * @author johnkim
 *
 */
public class DayveCategoryRest {

	public static final String TAG = DayveCategoryRest.class.getSimpleName();
	
	// 서버 주소.
	public static final String SERVER_URL = ExternalJoinConstant.MOBILE_SERVER_URL;
	
	// 컨트롤러 매핑.
	public static final String MYHISTORY_CATEGORY_CONTROLLER = "/myHistoryCategoryController";
	
	// 카테고리 중복 검사.
	public static final String MYHISTORY_CATEGORY_CHECK_DUPLICATION = "/checkDuplication";
	
	// 카테고리를 등록 한다.
	public static final String MYHISTORY_CATEGORY_REGISTER = "/registerCategory";
	
	// 카테고리 목록을 가져 온다.
	public static final String MYHISTORY_CATEGORY_LIST = "/selectCategoryList";
	
	// 카테고리에 등록된 item 목록을 가져 온다.
	public static final String MYHISTORY_CATEGORY_ITEM_LIST = "/selectItemListForCategoryAnalysis";
	
	// 사용자가 등록한 카테고리 전체 갯수를 가져 온다.
	public static final String MYHISTORY_CATEGORY_COUNT = "/selectCategoryCount";
	
	// 카테고리 상세 정보를 가져 온다.
	public static final String MYHISTORY_CATEGORY = "/selectCategory";
	
	// 카테고리 내용을 수정 한다.
	public static final String MYHISTORY_CATEGORY_MODIFY = "/updateCategory";
	
	// 카테고리를 비활성화 한다.
	public static final String MYHISTORY_CATEGORY_DEACTIVATE = "/updateDeactivateCategory";
	
	// 카테고리를 즐겨 찾기에 등록 한다.
	public static final String MYHISTORY_CATEGORY_FAVORITE = "/updateFavoriteCategory";
	
	// 카테고리를 즐겨 찾기에서 등록 해제 한다.
	public static final String MYHISTORY_CATEGORY_NOT_FAVORITE = "/updateDeactivateFavoriteCategory";
	
	/**
	 * 카테고리 등록 시 이름으로 중복 검사를 한다.
	 * 
	 * 등록되어 있는 카테고리 이름의 갯수를 가져 온다.
	 * 
	 * 1. 카테고리 이름이 등록되어 있지 않으면 : 0
	 * 2. 카테고리 이름이 이미 등록되어 있으면 : 1
	 * 3. 실행 중 오류 발생     : ERROR
	 * 
	 * @param dayveCategory
	 * @return 0 or 1 or ERROR
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory checkDuplication(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "checkDuplication");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("name", dayveCategory.getName());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_CHECK_DUPLICATION);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	mDayveCategory.setCount(mJsonCategory2.getString("count"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to check duplication category.");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "Fail to check duplication category.");
			mDayveCategory.setCount("ERROR");
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리를 등록 한다.
	 * 
	 * 등록 결과가 SUCCESS 나 FAIL 의 string 으로 반환 된다.
	 * 
	 * @param dayveCategory
	 * @return SUCCESS or FAIL
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory registerCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "registerCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("name", dayveCategory.getName());
		jsonCategory.put("comment", dayveCategory.getComment());
		jsonCategory.put("iconUrl", dayveCategory.getIconUrl());
		jsonCategory.put("openCategoryYn", dayveCategory.getOpenCategoryYn());
		jsonCategory.put("favoriteYn", dayveCategory.getFavoriteYn());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("modifyDate", dayveCategory.getModifyDate());
		jsonCategory.put("registerDate", dayveCategory.getRegisterDate());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_REGISTER);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	// SUCCESS or FAIL
	            	mDayveCategory.setResult(mJsonCategory2.getString("result"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to register category.");
		} catch (Exception e) {
			// TODO: handle exception
			mDayveCategory.setResult("FAIL");
			Log.e(TAG, "Fail to register category.");
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리 목록을 가져 온다.
	 * 
	 * @param dayveCategory
	 * @return ArrayList<DayveCategory>
	 * @throws JSONException
	 * @throws IOException
	 */
	public ArrayList<DayveCategory> selectCategoryList(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "selectCategoryList");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		jsonCategory.put("categoryStart", dayveCategory.getCategoryStart());
		jsonCategory.put("categoryCount", dayveCategory.getCategoryCount());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		ArrayList<DayveCategory> categoryList = new ArrayList<DayveCategory>();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_LIST);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONArray jsonArray = new JSONArray(line);
	            	
	            	Log.i(TAG, "jsonArray.length() : " + jsonArray.length());
	            	
	            	for(int i=0 ; i<jsonArray.length() ; i++) {
	            		JSONObject mJsonCategory = jsonArray.getJSONObject(i);
		            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
		            	
		            	DayveCategory mDayveCategory = new DayveCategory();
		            	
		            	mDayveCategory.setIdMyHistoryCategories(mJsonCategory2.getString("idMyHistoryCategories"));
		            	mDayveCategory.setName(mJsonCategory2.getString("name"));
		            	mDayveCategory.setComment(mJsonCategory2.getString("comment"));
		            	mDayveCategory.setIconUrl(mJsonCategory2.getString("iconUrl"));
		            	mDayveCategory.setOpenCategoryYn(mJsonCategory2.getString("openCategoryYn"));
		            	mDayveCategory.setFavoriteYn(mJsonCategory2.getString("favoriteYn"));
		            	mDayveCategory.setActivateYn(mJsonCategory2.getString("activateYn"));
		            	mDayveCategory.setModifyDate(mJsonCategory2.getString("modifyDate"));
		            	mDayveCategory.setRegisterDate(mJsonCategory2.getString("registerDate"));
		            	mDayveCategory.setMyHistoryMembersId(mJsonCategory2.getString("myHistoryMembersId"));
		            	mDayveCategory.setItemsCount(mJsonCategory2.getString("itemsCount"));
		            	
//		            	Log.i(TAG, "idMyHistoryCategories    		 : " + mDayveCategory.getIdMyHistoryCategories());
//		            	Log.i(TAG, "name                     		 : " + mDayveCategory.getName());
//		            	Log.i(TAG, "comment                    		 : " + mDayveCategory.getComment());
//		            	Log.i(TAG, "openCategoryYn           		 : " + mDayveCategory.getOpenCategoryYn());
//		            	Log.i(TAG, "favoriteYn               		 : " + mDayveCategory.getFavoriteYn());
//		            	Log.i(TAG, "activateYn               		 : " + mDayveCategory.getActivateYn());
//		            	Log.i(TAG, "modifyDate               		 : " + mDayveCategory.getModifyDate());
//		            	Log.i(TAG, "registerDate             		 : " + mDayveCategory.getRegisterDate());
//		            	Log.i(TAG, "groupName                		 : " + mDayveCategory.getGroupName());
//		            	Log.i(TAG, "groupUnit               		 : " + mDayveCategory.getGroupUnit());
//		            	Log.i(TAG, "groupUnitTime             		 : " + mDayveCategory.getGroupUnitTime());
//		            	Log.i(TAG, "myHistoryMembersId       		 : " + mDayveCategory.getMyHistoryMembersId());
//		            	Log.i(TAG, "itemsCount               		 : " + mDayveCategory.getItemsCount());
//		            	Log.i(TAG, "myHistoryGroupsIdMyHistoryGroups : " + mDayveCategory.getMyHistoryGroupsIdMyHistoryGroups());
		            	
		            	categoryList.add(mDayveCategory);
	            	}
	        	}
	        }
	        
	        Log.i(TAG, "Success to select category list.");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "Fail to select category list.");
		}
		
		return categoryList;
	}
	
	/**
	 * Category 에 등록된 item 목록을 가져 온다.
	 * 
	 * @param dayveItem
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public ArrayList<DayveItem> selectItemListForCategoryAnalysis(DayveItem dayveItem) throws JSONException, IOException {
		Log.i(TAG, " throws JSONException, IOException {");
		
		JSONObject jsonItem = new JSONObject();
		
		jsonItem.put("activateYn", dayveItem.getActivateYn());
		jsonItem.put("myHistoryMembersId", dayveItem.getMyHistoryMembersId());
//		jsonItem.put("myHistoryCategoriesIdMyHistoryCategories", dayveItem.getMyHistoryCategoriesIdMyHistoryCategories());
		
		String jsonString = "{\"myHistoryItem\":" + jsonItem.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		ArrayList<DayveItem> itemList = new ArrayList<DayveItem>();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_ITEM_LIST);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONArray jsonArray = new JSONArray(line);
	            	
	            	Log.i(TAG, "jsonArray.length() : " + jsonArray.length());
	            	
	            	for(int i=0 ; i<jsonArray.length() ; i++) {
	            		JSONObject mJsonItem = jsonArray.getJSONObject(i);
	            		JSONObject mJsonItem2 = mJsonItem.getJSONObject("myHistoryItem");
	            		
	            		DayveItem mDayItem = new DayveItem();
	            		
	            		mDayItem.setIdMyHistoryItems(mJsonItem2.getString("idMyHistoryItems"));
	            		mDayItem.setName(mJsonItem2.getString("name"));
	            		mDayItem.setComment(mJsonItem2.getString("comment"));
	            		mDayItem.setAmount(mJsonItem2.getString("amount"));
	            		mDayItem.setOpenItemYn(mJsonItem2.getString("openItemYn"));
	            		mDayItem.setFavoriteYn(mJsonItem2.getString("favoriteYn"));
	            		mDayItem.setActivateYn(mJsonItem2.getString("activateYn"));
	            		mDayItem.setModifyDate(mJsonItem2.getString("modifyDate"));
	            		mDayItem.setRegisterDate(mJsonItem2.getString("registerDate"));
	            		mDayItem.setMyHistoryMembersId(mJsonItem2.getString("myHistoryMembersId"));
	            		mDayItem.setMyHistoryCategoriesIdMyHistoryCategories(mJsonItem2.getString("myHistoryCategoriesIdMyHistoryCategories"));
	            		
	            		itemList.add(mDayItem);
	            	}
	        	}
	        }
	        
	        Log.i(TAG, "Success to select category-item list.");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "Fail to select category-item list.");
		}
		
		return itemList;
	}
	
	/**
	 * 사용자가 등록한 카테고리 전제 갯수를 가져 온다.
	 * 
	 * @param dayveCategory
	 * @return DayveCategory
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory selectCategoryCount(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "selectCategoryCount");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_COUNT);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	mDayveCategory.setCount(mJsonCategory2.getString("count"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to select category count.");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "Fail to select category count.");
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리 상세 정보를 가져 온다.
	 * 
	 * @param dayveCategory
	 * @return DayveCategory
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory selectCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "selectCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("idMyHistoryCategories", dayveCategory.getIdMyHistoryCategories());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	mDayveCategory.setIdMyHistoryCategories(mJsonCategory2.getString("idMyHistoryCategories"));
	            	mDayveCategory.setName(mJsonCategory2.getString("name"));
	            	mDayveCategory.setComment(mJsonCategory2.getString("comment"));
	            	mDayveCategory.setIconUrl(mJsonCategory2.getString("iconUrl"));
	            	mDayveCategory.setOpenCategoryYn(mJsonCategory2.getString("openCategoryYn"));
	            	mDayveCategory.setFavoriteYn(mJsonCategory2.getString("favoriteYn"));
	            	mDayveCategory.setActivateYn(mJsonCategory2.getString("activateYn"));
	            	mDayveCategory.setModifyDate(mJsonCategory2.getString("modifyDate"));
	            	mDayveCategory.setRegisterDate(mJsonCategory2.getString("registerDate"));
	            	mDayveCategory.setMyHistoryMembersId(mJsonCategory2.getString("myHistoryMembersId"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to select category.");
		} catch (Exception e) {
			// TODO: handle exception
			Log.e(TAG, "Fail to select category.");
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리 내용을 수정 한다.
	 * 수정 결과를 SUCCESS or FAIL 의 string 으로 반환 한다.
	 * 
	 * @param dayveCategory
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory updateCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "updateCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		Log.i(TAG, "idMyHistoryCategories : " + dayveCategory.getIdMyHistoryCategories());
		Log.i(TAG, "name                  : " + dayveCategory.getName());
		Log.i(TAG, "comment               : " + dayveCategory.getComment());
		Log.i(TAG, "iconUrl               : " + dayveCategory.getIconUrl());
		Log.i(TAG, "openCategoryYn        : " + dayveCategory.getOpenCategoryYn());
		Log.i(TAG, "favoriteYn            : " + dayveCategory.getFavoriteYn());
		Log.i(TAG, "activateYn            : " + dayveCategory.getActivateYn());
		Log.i(TAG, "modifyDate            : " + dayveCategory.getModifyDate());
		Log.i(TAG, "myHistoryMembersId    : " + dayveCategory.getMyHistoryMembersId());
		
		jsonCategory.put("idMyHistoryCategories", dayveCategory.getIdMyHistoryCategories());
		jsonCategory.put("name", dayveCategory.getName());
		jsonCategory.put("comment", dayveCategory.getComment());
		jsonCategory.put("iconUrl", dayveCategory.getIconUrl());
		jsonCategory.put("openCategoryYn", dayveCategory.getOpenCategoryYn());
		jsonCategory.put("favoriteYn", dayveCategory.getFavoriteYn());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("modifyDate", dayveCategory.getModifyDate());
//		jsonCategory.put("registerDate", dayveCategory.getRegisterDate());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_MODIFY);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	// SUCCESS or FAIL
	            	mDayveCategory.setResult(mJsonCategory2.getString("result"));
	        	}
	        }
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리를 비활성화 한다.
	 * 수정 결과는 SUCCESS 나 FAIL 의 string 으로 반환 한다.
	 * 
	 * @param dayveCategory
	 * @return DayveCategory
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory updateDeactivateCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "updateDeactivateCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("idMyHistoryCategories", dayveCategory.getIdMyHistoryCategories());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("modifyDate", dayveCategory.getModifyDate());
//		jsonCategory.put("registerDate", dayveCategory.getRegisterDate());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_DEACTIVATE);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	// SUCCESS or FAIL
	            	mDayveCategory.setResult(mJsonCategory2.getString("result"));
	        	}
	        }
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리를 즐겨 찾기에 등록 한다.
	 * 등록 결과는 SUCCESS 나 FAIL 의 string 으로 반환 한다.
	 * 
	 * @param dayveCategory
	 * @return DayveCategory
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory updateFavoriteCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "updateFavoriteCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("idMyHistoryCategories", dayveCategory.getIdMyHistoryCategories());
		jsonCategory.put("favoriteYn", dayveCategory.getFavoriteYn());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("modifyDate", dayveCategory.getModifyDate());
		jsonCategory.put("registerDate", dayveCategory.getRegisterDate());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_FAVORITE);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	// SUCCESS or FAIL
	            	mDayveCategory.setResult(mJsonCategory2.getString("result"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to update favorite category.");
		} catch (Exception e) {
			// TODO: handle exception
			mDayveCategory.setResult("FAIL");
			Log.e(TAG, "Fail to update favorite category.");
		}
		
		return mDayveCategory;
	}
	
	/**
	 * 카테고리를 즐겨 찾기 등록에서 해제 한다.
	 * 수정 결과를 SUCCESS 나 FAIL 의 string 으로 반환 한다.
	 * 
	 * @param dayveCategory
	 * @return
	 * @throws JSONException
	 * @throws IOException
	 */
	public DayveCategory updateDeactivateFavoriteCategory(DayveCategory dayveCategory) throws JSONException, IOException {
		Log.i(TAG, "updateDeactivateFavoriteCategory");
		
		JSONObject jsonCategory = new JSONObject();
		
		jsonCategory.put("idMyHistoryCategories", dayveCategory.getIdMyHistoryCategories());
		jsonCategory.put("activateYn", dayveCategory.getActivateYn());
		jsonCategory.put("modifyDate", dayveCategory.getModifyDate());
		jsonCategory.put("registerDate", dayveCategory.getRegisterDate());
		jsonCategory.put("myHistoryMembersId", dayveCategory.getMyHistoryMembersId());
		
		String jsonString = "{\"myHistoryCategory\":" + jsonCategory.toString() + "}";
		
		Log.i(TAG, "jsonCategory : " + jsonString);
		
		DayveCategory mDayveCategory = new DayveCategory();
		
		try {
			URL postUrl = new URL(SERVER_URL + MYHISTORY_CATEGORY_CONTROLLER + MYHISTORY_CATEGORY_NOT_FAVORITE);
			HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
	        connection.setDoOutput(true);
	        connection.setInstanceFollowRedirects(false);
	        connection.setRequestMethod("POST");
	        connection.setRequestProperty("Content-Type", "application/json");
	        OutputStream os = connection.getOutputStream();
	        os.write(jsonString.getBytes());
	        os.flush();
	        
	        Log.i(TAG, "response code : " + connection.getResponseCode());
	        
	        if(connection != null) {
	        	connection.setConnectTimeout(3000);
	        	
	        	if(connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
	        		BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
	            	
	            	String line = br.readLine();
	            	
	            	JSONObject mJsonCategory = new JSONObject(line);
	            	JSONObject mJsonCategory2 = mJsonCategory.getJSONObject("myHistoryCategory");
	            	
	            	// SUCCESS or FAIL
	            	mDayveCategory.setResult(mJsonCategory2.getString("result"));
	        	}
	        }
	        
	        Log.i(TAG, "Success to update deactivate favorite category.");
		} catch (Exception e) {
			// TODO: handle exception
			mDayveCategory.setResult("FAIL");
			Log.e(TAG, "Fail to update deactivate favorite category.");
		}
		
		return mDayveCategory;
	}
}
